package com.bree.erp.jsf;

import java.util.List;

import javax.ejb.EJB;

import com.bree.erp.entity.Project;
import com.bree.erp.service.ProjectService;

public class ProjectBean extends Project {

	@EJB 
	private ProjectService service;
	
	public String create() {
		
		service.create(getCode(), getDescription());
		
		return "back";
	}
	
	/**
	 * Load project entity for editing.
	 * 
	 * @param id
	 * @return
	 */
	public String edit(long id) {
		
		Project project = service.read(id);
		
		this.setId(project.getId());
		this.setCode(project.getCode());
		this.setDescription(project.getDescription());
		
		// go to edit page.
		return "update";
	}
	
	/**
	 * Save and update project entity.
	 * 
	 * @return
	 */
	public String update() {
		
		service.update(getId(), getCode(), getDescription());
		
		// return to main page.
		return "back";
	}
	
	public String delete(long id) {
		
		service.delete(id);
		
		return "same";
	}
	
	public List<Project> getProjects() {
		return service.list();
	}
}
